package com.qf.dao.impl;


import com.qf.dao.AccountDao;
import com.qf.pojo.Account;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/8/30 23:58
 * @Pacakge: com.qf.dao.impl
 * @ClassName: AccountDaoImpl
 * @Author: fan
 */
public class AccountDaoImpl implements AccountDao {
    private Connection connection ;
    private PreparedStatement preparedStatement;
    private Statement statement;
    private ResultSet resultSet;
    @Override
    public List<Account> findAllAccount() throws Exception {

            //1>导包
            //2>注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //3>获取数据库连接对象
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/spring",
                    "root",
                    "f971014"
            );
            //4>准备sql语句
            String sql = "select * from account";
            //5>通过数据库连接对象获取执行对象
            statement = connection.createStatement();
            //6>执行预编译对象
            resultSet = statement.executeQuery(sql);
            //7>建立一个集合,将其结果放入到里面
            List<Account> accounts = new ArrayList<>();
            while(resultSet.next()){
               //创建Account对象
                Account account = new Account();
                account.setId(resultSet.getInt("id"));
                account.setName(resultSet.getString("name"));
                account.setMoney(resultSet.getDouble("money"));
                accounts.add(account);
            }
            accounts.stream().forEach(o->System.out.println(o));
            //8>释放资源
            resultSet.close();
            statement.close();
            connection.close();
            return accounts;


    }

    @Override
    public Account findAccountById(Integer accountId) {
        return null;
    }

    @Override
    public void saveAccount(Account account) {

    }

    @Override
    public void updateAccount(Account account) {

    }

    @Override
    public void deleteAccount(Integer accountId) {

    }
}
